package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author theme
 * @date 2020-05-27
 */
@Api(description = "【请填写功能名称】Controller" )
@RestController
@RequestMapping("/system/company" )
public class SysCompanyController extends BaseController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISysCompanyService sysCompanyService;

    @Autowired
    private ISysCombankService sysCombankService;

    @Autowired
    private ISysComdictService sysComdictService;

    @Autowired
    private ISysBankService sysBankService;


    @Autowired
    private ISysExchangeService sysExchangeService;

/**
 * 查询【请填写功能名称】列表
 */
@ApiOperation("查询公司简介列表" )
@PreAuthorize("@ss.hasPermi('system:company:list')" )
@GetMapping("/list" )
        public TableDataInfo list(SysCompany sysCompany) {
        startPage();
        List<SysCompany> list = sysCompanyService.selectSysCompanyList(sysCompany);
        return getDataTable(list);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation("获取【请填写功能名称】详细信息" )
    @PreAuthorize("@ss.hasPermi('system:company:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        SysCompany sysCompany=sysCompanyService.selectSysCompanyById(id);
        return AjaxResult.success(sysCompany);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PostMapping(value = "/getBankUsers" )
    public AjaxResult getBankUsers(@RequestBody SysCombank sysCombank) {
        List<SysBank> lists=new ArrayList<>();
        List<SysCombank> list=sysCombankService.selectSysCombankList(sysCombank);
        if(!list.isEmpty()){
            for(SysCombank data:list){
                SysBank sysBank= sysBankService.selectSysBankById(data.getBankUserId());
                lists.add(sysBank);
            }
        }
        return AjaxResult.success(lists);
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @PostMapping(value = "/getBankDicts" )
    public AjaxResult getBankDicts(@RequestBody SysComdict sysComdict) {
        List<SysComdict> list=sysComdictService.selectSysComdictList(sysComdict);
    /*    if(!list.isEmpty()){
            List<Long> ids=list.stream().map(SysComdict::getDictCode).collect(Collectors.toList());
            return AjaxResult.success(ids);
        }*/
        return AjaxResult.success(list);
    }
    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation("新增【请填写功能名称】" )
    @PreAuthorize("@ss.hasPermi('system:company:add')" )
    @Log(title = "活动新增" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCompany sysCompany) {
        sysCompany.setUpdateTime(new Date());
        return toAjax(sysCompanyService.insertSysCompany(sysCompany));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation("修改【请填写功能名称】" )
    @PreAuthorize("@ss.hasPermi('system:company:edit')" )
    @Log(title = "活动修改" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCompany sysCompany) {
        if(null!=sysCompany.getFileList()&&!sysCompany.getFileList().isEmpty()){
            sysCompany.setRewards(sysCompany.getFileList().stream().map(SysImg::getUrl).collect(Collectors.joining(",")));
        }
       int flag= sysCompanyService.updateSysCompany(sysCompany);
       if(flag>0){
           SysCompany company=sysCompanyService.selectSysCompanyById(sysCompany.getId());
           redisTemplate.opsForValue().set(company.getId()+"",company.getChance());
       }
        return toAjax(flag);
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation("删除【请填写功能名称】" )
    @PreAuthorize("@ss.hasPermi('system:company:remove')" )
    @Log(title = "活动删除" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{details}" )
    public AjaxResult remove(@PathVariable String[] details) {
       /* String companyId=details[0];
        SysExchange query=new SysExchange();
        query.setCompanyId(Long.valueOf(companyId));
        query.setDeleted(0l);
        query.setStatus("0");
        List<SysExchange> list=sysExchangeService.selectSysExchangeList(query);
        if(!list.isEmpty()){
            return AjaxResult.error("该活动内有启用的兑换码且未被领用，该活动不支持删除!");
        }*/
        return toAjax(sysCompanyService.deleteSysCompanyByIds(details));
    }


    @PostMapping("/addBankUser" )
    @Transactional
    public AjaxResult addBankUser(@RequestBody SysCombank sysCombank) {
        sysCombankService.deleteSysCombankById(sysCombank.getCompanyId());
        if(null!=sysCombank.getIds()&&sysCombank.getIds().length>0){
            for(Long bankUserId:sysCombank.getIds()){
                SysCombank data=new SysCombank();
                data.setCompanyId(sysCombank.getCompanyId());
                data.setBankUserId(bankUserId);
                sysCombankService.insertSysCombank(data);
            }
        }
        return AjaxResult.success();
    }




    @PostMapping("/addBankDicts" )
    @Transactional
    public AjaxResult addBankDicts(@RequestBody SysComdict sysComdict) {
        if(sysComdict.getLists().isEmpty()){
            return AjaxResult.error("请选择立减金!");
        }
        sysComdictService.deleteSysComdictById(sysComdict.getCompanyId());
        if(!sysComdict.getLists().isEmpty()){
            for(SysComdict data:sysComdict.getLists()){
                SysComdict datas=new SysComdict();
                datas.setCompanyId(sysComdict.getCompanyId());
                datas.setDictCode(data.getDictCode());
                datas.setChance(data.getChance());
                sysComdictService.insertSysComdict(datas);
            }
        }
        return AjaxResult.success();
    }

    @PostMapping("/addBankDict" )
    @Transactional
    public AjaxResult addBankDict(@RequestBody SysComdict sysComdict) {
        sysComdictService.deleteSysComdictById(sysComdict.getCompanyId());
        if(null!=sysComdict.getIds()&&sysComdict.getIds().length>0){
            for(Long dictId:sysComdict.getIds()){
                SysComdict data=new SysComdict();
                data.setCompanyId(sysComdict.getCompanyId());
                data.setDictCode(dictId);
                sysComdictService.insertSysComdict(data);
            }
        }
        return AjaxResult.success();
    }

}
